package com.atguigu.service.impl;

import com.atguigu.entity.PlatformPropertyKey;
import com.atguigu.entity.PlatformPropertyValue;
import com.atguigu.entity.ProductSalePropertyValue;
import com.atguigu.mapper.PlatformPropertyKeyMapper;
import com.atguigu.service.PlatformPropertyKeyService;
import com.atguigu.service.PlatformPropertyValueService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 属性表 服务实现类
 * </p>
 *
 * @author fumeng
 * @since 2021-10-28
 */
@Service
public class PlatformPropertyKeyServiceImpl extends ServiceImpl<PlatformPropertyKeyMapper, PlatformPropertyKey> implements PlatformPropertyKeyService {

    @Autowired
    PlatformPropertyValueService propertyValueService;
    @Override
    public List<PlatformPropertyKey> getPlatformPropertyByCategoryId(Long category1Id, Long category2Id, Long category3Id) {
        return baseMapper.getPlatformPropertyByCategoryId(category1Id,category2Id,category3Id);

    }

    //保存和修改
    @Override
    @Transactional
    public void savePlatformProperty(PlatformPropertyKey platformPropertyKey) {
        if(platformPropertyKey.getId() != null){
            baseMapper.updateById(platformPropertyKey);
            //先删除平台属性在添加
            QueryWrapper<PlatformPropertyValue> wrapper = new QueryWrapper<>();
            wrapper.eq("property_key_id",platformPropertyKey.getId());
            propertyValueService.remove(wrapper);
        }else{
            //保存数据到平台属性值表
            baseMapper.insert(platformPropertyKey);
        }
        //保存数据到平台属性值表
        List<PlatformPropertyValue> propertyValueList = platformPropertyKey.getPropertyValueList();
        if(propertyValueList != null){
            for (PlatformPropertyValue propertyValue : propertyValueList) {
                propertyValue.setPropertyKeyId(platformPropertyKey.getId());
            }
            propertyValueService.saveBatch(propertyValueList);
        }
    }

    @Override
    public List<PlatformPropertyKey> getPlatformPropertyBySkuId(Long skuId) {

        return baseMapper.getPlatformPropertyBySkuId(skuId);
    }

}
